<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/3/28
 * Time: 上午9:03
 */

namespace app\common\controller;
use app\common\model\Agent;
use app\common\model\CardGeneral;
use app\common\model\CardMember;
use app\common\model\Merchant;
use app\common\model\Momerchant;
use app\common\model\OpenTemplate;
use app\common\model\System;
use app\common\model\WxOpen;
use think\Controller;
class Platurl extends Controller
{

    //获取所属代理商的链接
    public function getagenturl($type,$param){

        $CardMember = new CardMember();
        $Merchant = new Merchant();
        $Agent=new Agent();
        $Momerchant = new Momerchant();
        switch($type){
            case "card_id":
                $mid = $CardMember->where('card_id', $param)->value('mid');
                if (!$mid){
                    $CardGeneral = new CardGeneral();
                    $mid = $CardGeneral->where('card_id', $param)->value('mid');
                }
                $agent_id = $Merchant->where(['id'=>$mid])->value('agent');
                $agent_url = $Agent->where(['id'=>$agent_id])->value('open_url');
                break;
            case "top_mid":
                $agent_id = $Momerchant->where('id', $param)->value('agent');
                $agent_url = $Agent->where(['id'=>$agent_id])->value('open_url');
                break;
            case "mid":
                $agent_id = $Merchant->where(['id'=>$param])->value('agent');
                $agent_url = $Agent->where(['id'=>$agent_id])->value('open_url');
                break;
            case "agent_id";
                $agent_url = $Agent->where(['id'=>$param])->value('open_url');
                break;
        }
        return 'https://'.$agent_url;

    }

    //获取所属代理商的模板消息通知公众号
    public function getagenttplmsg($mid){
        $Merchant = new Merchant();
        $Agent = new Agent();
        $WxOpen = new WxOpen();
        $System = new System();
        $mach = $Merchant->where(['id'=>$mid])->field('agent,wx_open_id,tpl_msg_type')->find();
        $agent = $Agent->where(['id'=>$mach['agent']])->field('tpl_msg_type,wx_open_id')->find();
        switch ($mach['tpl_msg_type']){
            case "0":           //原系统公众号通知
                $sysconfig=$System->where(array('id'=>1))->find();
                $config=$Agent->where(array('id'=>$sysconfig['sys_agent_id']))->field('component_token,component_encodingaeskey,component_appid,component_appsecret,open_ticket')->find();
                $users = $WxOpen->where(array('id' => $sysconfig['wx_open_id']))->field('authorizer_appid,authorizer_refresh_token')->find();
                break;
//            case "1":           //系统公众号通知
//                $config = $System->where(array('id' => 1))->field('component_token,component_encodingaeskey,component_appid,component_appsecret,open_ticket,wx_open_id')->find();
//                $users = $WxOpen->where(array('id' => $config['wx_open_id']))->field('authorizer_appid,authorizer_refresh_token')->find();
//                break;
            case "2":           //代理商公众号通知
                $config=$Agent->where(array('id'=>$mach['agent']))->field('component_token,component_encodingaeskey,component_appid,component_appsecret,open_ticket')->find();
                $users = $WxOpen->where(array('id' => $agent['wx_open_id']))->field('authorizer_appid,authorizer_refresh_token')->find();
                break;
            case "3":           //商户公众号通知
                $config = $Agent->where(array('id' => $mach['agent']))->field('component_token,component_encodingaeskey,component_appid,component_appsecret,open_ticket')->find();
                $users = $WxOpen->where(array('id' => $mach['wx_open_id']))->field('authorizer_appid,authorizer_refresh_token')->find();
                break;
            default:
                $config=$Agent->where(array('id'=>$mach['agent']))->field('component_token,component_encodingaeskey,component_appid,component_appsecret,open_ticket')->find();
                $users = $WxOpen->where(array('id' => $agent['wx_open_id']))->field('authorizer_appid,authorizer_refresh_token')->find();
        }
        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        if ($options['component_token'] && $options['component_encodingaeskey'] && $options['component_appid'] && $options['component_appsecret'] && $options['open_ticket'] && $options['authorizer_appid'] && $options['authorizer_refresh_token']){
            return $options;
        }else{
            return false;
        }

    }

    //获取所属代理商的模板消息发送类型
    public function getagenttplmsgtype($mid){
        $Merchant = new Merchant();
        $tplmsgtype = $Merchant->where(['id'=>$mid])->value('tpl_msg_type');
        return $tplmsgtype;

    }

    public function getagenttplid($mid){
        $Merchant = new Merchant();
        $Agent = new Agent();
        $System = new System();
        $OpenTemplate = new OpenTemplate();
        $mach = $Merchant->where(['id'=>$mid])->field('wx_open_id,agent,tpl_msg_type')->find();
        $agent = $Agent->where(['id'=>$mach['agent']])->field('tpl_msg_type,wx_open_id')->find();
        switch ($mach['tpl_msg_type']){
            case "0":           //原系统公众号通知
                $config = $System->where(array('id' => 1))->field('wx_open_id')->find();
                $tpl_id = $OpenTemplate->where(['type' => 'pre_man_tpl', 'wx_open_id' => $config['wx_open_id']])->value('template_id');
                break;
//            case "1":           //系统公众号通知
//                $config = $System->where(array('id' => 1))->field('wx_open_id')->find();
//                $tpl_id = $OpenTemplate->where(['type' => 'pre_man_tpl', 'wx_open_id' => $config['wx_open_id']])->value('template_id');
//                break;
            case "2":           //代理商公众号通知
                $tpl_id = $OpenTemplate->where(['type' => 'pre_man_tpl', 'wx_open_id' => $agent['wx_open_id']])->value('template_id');
                break;
            case "3":           //商户公众号通知
                $tpl_id = $OpenTemplate->where(['type' => 'pre_man_tpl', 'wx_open_id' => $mach['wx_open_id']])->value('template_id');
                break;
            default:
                $config = $System->where(array('id' => 1))->field('wx_open_id')->find();
                $tpl_id = $OpenTemplate->where(['type' => 'pre_man_tpl', 'wx_open_id' => $config['wx_open_id']])->value('template_id');
                break;
        }
        if ($tpl_id){
            return $tpl_id;
        }else{
            return false;
        }

    }

    public function getagentpaytplid($mid){
        $Merchant = new Merchant();
        $Agent = new Agent();
        $System = new System();
        $OpenTemplate = new OpenTemplate();
        $mach = $Merchant->where(['id'=>$mid])->field('wx_open_id,agent,tpl_msg_type')->find();
        $agent = $Agent->where(['id'=>$mach['agent']])->field('tpl_msg_type,wx_open_id')->find();
        switch ($mach['tpl_msg_type']){
            case "0":           //原系统公众号通知
                $config = $System->where(array('id' => 1))->field('wx_open_id')->find();
                $tpl_id = $OpenTemplate->where(['type' => 'pay_tpl', 'wx_open_id' => $config['wx_open_id']])->value('template_id');
                break;
//            case "1":           //系统公众号通知
//                $config = $System->where(array('id' => 1))->field('wx_open_id')->find();
//                $tpl_id = $OpenTemplate->where(['type' => 'pay_tpl', 'wx_open_id' => $config['wx_open_id']])->value('template_id');
//                break;
            case "2":           //代理商公众号通知
                $tpl_id = $OpenTemplate->where(['type' => 'pay_tpl', 'wx_open_id' => $agent['wx_open_id']])->value('template_id');
                break;
            case "3":           //商户公众号通知
                $tpl_id = $OpenTemplate->where(['type' => 'pay_tpl', 'wx_open_id' => $mach['wx_open_id']])->value('template_id');
                break;
            default:
                $config = $System->where(array('id' => 1))->field('wx_open_id')->find();
                $tpl_id = $OpenTemplate->where(['type' => 'pay_tpl', 'wx_open_id' => $config['wx_open_id']])->value('template_id');
                break;
        }
        if ($tpl_id){
            return $tpl_id;
        }else{
            return false;
        }

    }
}